<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>查询表达式 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/query-dsl-intro.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/query-dsl-intro.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/query-dsl-intro.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/query-dsl-intro.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="getting-started.html">基础入门</a></span>
»
<span class="breadcrumb-link"><a href="full-body-search.html">请求体查询</a></span>
»
<span class="breadcrumb-node">查询表达式</span>
</div>
<div class="navheader">
<span class="prev">
<a href="_empty_search.html">« 空查询</a>
</span>
<span class="next">
<a href="_queries_and_filters.html">查询与过滤 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="query-dsl-intro"></a>查询表达式<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/054_Query_DSL/60_Query_DSL.asciidoc">edit</a>
</h2>
</div></div></div>
<p>查询表达式(Query DSL)是一种非常灵活又富有表现力的  查询语言。 Elasticsearch 使用它可以以简单的 JSON 接口来展现 Lucene 功能的绝大部分。在你的应用中，你应该用它来编写你的查询语句。它可以使你的查询语句更灵活、更精确、易读和易调试。</p>
<p>要使用这种查询表达式，只需将查询语句传递给 <code class="literal">query</code> 参数：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">GET /_search
{
    "query": YOUR_QUERY_HERE
}</pre>
</div>
<p><em>空查询（empty search）</em> —<code class="literal">{}</code>— 在功能上等价于使用 <code class="literal">match_all</code> 查询， 正如其名字一样，匹配所有文档：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">GET /_search
{
    "query": {
        "match_all": {}
    }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/054_Query_DSL/60_Empty_query.json"></div>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_查询语句的结构"></a>查询语句的结构<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/054_Query_DSL/60_Query_DSL.asciidoc">edit</a>
</h3>
</div></div></div>
<p>一个查询语句的典型结构：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
    QUERY_NAME: {
        ARGUMENT: VALUE,
        ARGUMENT: VALUE,...
    }
}</pre>
</div>
<p>如果是针对某个字段，那么它的结构如下：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
    QUERY_NAME: {
        FIELD_NAME: {
            ARGUMENT: VALUE,
            ARGUMENT: VALUE,...
        }
    }
}</pre>
</div>
<p>举个例子，你可以使用 <code class="literal">match</code> 查询语句 来查询 <code class="literal">tweet</code> 字段中包含 <code class="literal">elasticsearch</code> 的 tweet：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
    "match": {
        "tweet": "elasticsearch"
    }
}</pre>
</div>
<p>完整的查询请求如下：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">GET /_search
{
    "query": {
        "match": {
            "tweet": "elasticsearch"
        }
    }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/054_Query_DSL/60_Match_query.json"></div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_合并查询语句"></a>合并查询语句<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/054_Query_DSL/60_Query_DSL.asciidoc">edit</a>
</h3>
</div></div></div>
<p><em>查询语句(Query clauses)</em> 就像一些简单的组合块，这些组合块可以彼此之间合并组成更复杂的查询。这些语句可以是如下形式：</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
<em>叶子语句（Leaf clauses）</em> (就像 <code class="literal">match</code> 语句)  被用于将查询字符串和一个字段（或者多个字段）对比。
</li>
<li class="listitem">
<em>复合(Compound)</em> 语句 主要用于  合并其它查询语句。
比如，一个 <code class="literal">bool</code> 语句 允许在你需要的时候组合其它语句，无论是  <code class="literal">must</code> 匹配、 <code class="literal">must_not</code> 匹配还是 <code class="literal">should</code> 匹配，同时它可以包含不评分的过滤器（filters）：
</li>
</ul>
</div>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">{
    "bool": {
        "must":     { "match": { "tweet": "elasticsearch" }},
        "must_not": { "match": { "name":  "mary" }},
        "should":   { "match": { "tweet": "full text" }},
        "filter":   { "range": { "age" : { "gt" : 30 }} }
    }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/054_Query_DSL/60_Bool_query.json"></div>
<p>一条复合语句可以合并 <em>任何</em> 其它查询语句，包括复合语句，了解这一点是很重要的。这就意味着，复合语句之间可以互相嵌套，可以表达非常复杂的逻辑。</p>
<p>例如，以下查询是为了找出信件正文包含 <code class="literal">business opportunity</code> 的星标邮件，或者在收件箱正文包含  <code class="literal">business opportunity</code> 的非垃圾邮件：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
    "bool": {
        "must": { "match":   { "email": "business opportunity" }},
        "should": [
            { "match":       { "starred": true }},
            { "bool": {
                "must":      { "match": { "folder": "inbox" }},
                "must_not":  { "match": { "spam": true }}
            }}
        ],
        "minimum_should_match": 1
    }
}</pre>
</div>
<p>到目前为止，你不必太在意这个例子的细节，我们会在后面详细解释。最重要的是你要理解到，一条复合语句可以将多条语句 — 叶子语句和其它复合语句 — 合并成一个单一的查询语句。</p>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="_empty_search.html">« 空查询</a>
</span>
<span class="next">
<a href="_queries_and_filters.html">查询与过滤 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>